﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Mondial2014.Models.Competition;
using Mondial2014.Models.Context;
using Mondial2014.Utils;
using Mondial2014.Utils.Events;
using Mondial2014.Utils.Extensions;

namespace Mondial2014.Controllers
{
    public class QualifiersController : Controller
    {
        private readonly Mondial2014DbContext m_Context = new Mondial2014DbContext();

        public class QualifiersViewModel
        {
            public bool Admin { get; set; }
            public IEnumerable<string> Last16S { get; set; }
            public IEnumerable<string> Quarters { get; set; }
            public IEnumerable<string> Semis { get; set; }
            public IEnumerable<string> Finals { get; set; }
            public string Winner { get; set; }
        }

        // GET: Qualifiers
        public ActionResult Index(int? id)
        {
            var model = new QualifiersViewModel
            {
                Admin = (id.HasValue && id.Value == 1234),
                Last16S = m_Context.Teams.Where(t => t.Last16).Select(t => t.Name),
                Quarters = m_Context.Teams.Where(t => t.Quarter).Select(t => t.Name),
                Semis = m_Context.Teams.Where(t => t.Semi).Select(t => t.Name),
                Finals = m_Context.Teams.Where(t => t.Final).Select(t => t.Name),
                Winner = m_Context.Teams.Where(t => t.Winner).Select(t => t.Name).SingleOrDefault()
            };
            return View(model);
        }

        public ActionResult Edit()
        {
            return View(m_Context.Teams.ToList());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(IEnumerable<Team> teams)
        {
            var events = new Collection<KnockoutsStageUpdatedEvent>();
            var enumerable = teams as Team[] ?? teams.ToArray();
            if (ModelState.IsValid)
            {
                foreach (var team in enumerable)
                {
                    var currentTeamData = m_Context.Teams.Single(t => t.Id == team.Id);
                    if (currentTeamData.Last16 != team.Last16)
                    {
                        currentTeamData.Last16 = team.Last16;
                        events.Add(new KnockoutsStageUpdatedEvent(team.Id, StageType.Last16));
                    }
                    if (currentTeamData.Quarter != team.Quarter)
                    {
                        currentTeamData.Quarter = team.Quarter;
                        events.Add(new KnockoutsStageUpdatedEvent(team.Id, StageType.Quarter));
                    }
                    if (currentTeamData.Semi != team.Semi)
                    {
                        currentTeamData.Semi = team.Semi;
                        events.Add(new KnockoutsStageUpdatedEvent(team.Id, StageType.Semi));
                    }
                    if (currentTeamData.Final != team.Final)
                    {
                        currentTeamData.Final = team.Final;
                        events.Add(new KnockoutsStageUpdatedEvent(team.Id, StageType.Final));
                    }
                    if (currentTeamData.Winner != team.Winner)
                    {
                        currentTeamData.Winner = team.Winner;
                        events.Add(new KnockoutsStageUpdatedEvent(team.Id, StageType.Winner));
                    }
                }
                m_Context.SaveChanges();

                foreach (var knockoutsStageUpdatedEvent in events)
                {
                    EventDispatcher.PostEvent(knockoutsStageUpdatedEvent);
                }
                
                return RedirectToAction("Index");
            }
            return View(enumerable.ToList());
        }
    }
}